﻿using BaseUtils.Utils;
using NWMES.Mes;
using NWMES.Model;
using SharpConfig;
using System;
using System.Windows.Forms;

namespace NWMES.UI
{
    public partial class SettingForm : Form
    {
        public Configuration cfg;
        public SettingForm()
        {
            InitializeComponent();
            cfg = MesBaseEntity.config;
        }

        private void SettingForm_Load(object sender, System.EventArgs e)
        {
            foreach (var item in cfg)
            {
                if (item.Name.Contains("SharpConfigDefaultSection"))
                {
                    continue;
                }
                comboBox1.Items.Add(item.Name);
            }
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            ShowParams();
        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //string Section = comboBox1.Text;
            //if (string.IsNullOrEmpty(Section))
            //{
            //    MessageBox.Show("请先选择接口");
            //    return;
            //}
            //string Path = AppDomain.CurrentDomain.BaseDirectory + "\\MESCFG.ini";
            //IniHelper.IniWriteValue(Section, "Url", _Url.Text, Path);
            //IniHelper.IniWriteValue(Section, "UserName", UserName.Text, Path);
            //IniHelper.IniWriteValue(Section, "Password", Password.Text, Path);
            //IniHelper.IniWriteValue(Section, "User", User.Text, Path);
            //IniHelper.IniWriteValue(Section, "Timeout", _Timeout.Text, Path);
            //IniHelper.IniWriteValue(Section, "Site", _Site.Text, Path);
            //IniHelper.IniWriteValue(Section, "ActivityId", ActivityId.Text, Path);
            //IniHelper.IniWriteValue(Section, "Operation", _Operation.Text, Path);
            //IniHelper.IniWriteValue(Section, "OperationRevision", OperationRevision.Text, Path);
            //IniHelper.IniWriteValue(Section, "Resource", Resource.Text, Path);
            //IniHelper.IniWriteValue(Section, "DcGroup", DcGroup.Text, Path);
            //IniHelper.IniWriteValue(Section, "DcGroupRevision", DcGroupRevision.Text, Path);
            //IniHelper.IniWriteValue(Section, "ModeProcessSFC", ModeProcessSFC.Text, Path);
            //IniHelper.IniWriteValue(Section, "dcGroupSequence", dcGroupSequence.Text, Path);
            //IniHelper.IniWriteValue(Section, "dcMode", dcMode.Text, Path);
            //IniHelper.IniWriteValue(Section, "material", material.Text, Path);
            //IniHelper.IniWriteValue(Section, "materialRevision", materialRevision.Text, Path);

            string Section = comboBox1.Text;
            if (string.IsNullOrEmpty(Section))
            {
                MessageBox.Show("请先选择接口");
                return;
            }

            MesBaseEntity mesBaseEntity = new MesBaseEntity();
            mesBaseEntity.Url = _Url.Text;
            mesBaseEntity.UserName = UserName.Text;
            mesBaseEntity.Password = Password.Text;
            mesBaseEntity.User = User.Text;
            mesBaseEntity.Timeout = int.Parse(_Timeout.Text);
            mesBaseEntity.Site = _Site.Text;
            mesBaseEntity.ActivityId = _Url.Text;
            mesBaseEntity.Operation = _Operation.Text;
            mesBaseEntity.OperationRevision = OperationRevision.Text;
            mesBaseEntity.Resource = Resource.Text;
            mesBaseEntity.DcGroup = DcGroup.Text;
            mesBaseEntity.DcGroupRevision = DcGroupRevision.Text;
            mesBaseEntity.ModeProcessSFC = ModeProcessSFC.Text;
            mesBaseEntity.dcGroupSequence = dcGroupSequence.Text;
            mesBaseEntity.dcMode = dcMode.Text;
            mesBaseEntity.material = material.Text;
            mesBaseEntity.materialRevision = materialRevision.Text;

            new MESBaseService().UpdateMesRequestParams(Section, mesBaseEntity);
        }

        private void refresh_Click(object sender, EventArgs e)
        {
            MesBaseEntity.RefreshConfig();
            cfg = MesBaseEntity.config;
            ShowParams();
        }

        private void ShowParams()
        {
            if (string.IsNullOrEmpty(comboBox1.Text))
            {
                MessageBox.Show("请先选择接口");
                return;
            }
            Console.WriteLine();
            MESBaseService Params = cfg[comboBox1.Text].ToObject<MESBaseService>();
            _Url.Text = Params.Url;
            UserName.Text = Params.UserName;
            Password.Text = Params.Password;
            User.Text = Params.User;
            _Timeout.Text = Params.Timeout.ToString();
            _Site.Text = Params.Site;
            ActivityId.Text = Params.ActivityId;
            _Operation.Text = Params.Operation;
            OperationRevision.Text = "\"" + Params.OperationRevision + "\"";
            Resource.Text = Params.Resource;
            DcGroup.Text = Params.DcGroup;
            DcGroupRevision.Text = "\"" + Params.DcGroupRevision + "\"";
            ModeProcessSFC.Text = Params.ModeProcessSFC;
            dcGroupSequence.Text = Params.dcGroupSequence;
            dcMode.Text = Params.dcMode;
            material.Text = Params.material;
            materialRevision.Text = Params.materialRevision;
        }
    }
}
